﻿Imports System.Collections.Generic

Partial Class WebForm_SearchRoleControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Visible = True
    End Sub

    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        RaiseEvent Saving(Me, New EventArgs)
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub
End Class

Partial Class WebForm_SearchRoleControl
    Public Sub Show()
        Me.BindGV()

        Me.Visible = True
    End Sub

    Private Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select r.* ")
        sql.AppendFormat("from [Role] r ")
        sql.AppendFormat("where r.Active = 'Y' ")
        sql.AppendFormat("and r.Virtual = 'N' ")
        sql.AppendFormat("and r.Parent in ({0}) ", clsAuth.GetRoleList())
        Me.gv.DataSource = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt")
        Me.gv.DataBind()

        Me.mpSearch.Show()
    End Sub

    Public Sub Show(ByVal userID As String)
        Me.BindGV(userID)

        Me.Visible = True
    End Sub

    Private Sub BindGV(ByVal userID As String)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select r.* ")
        sql.AppendFormat("from [Role] r ")
        sql.AppendFormat("left join RoleUserMapping ru on r.RoleID = ru.RoleID and ru.UserID = '{0}' ", userID)
        sql.AppendFormat("where r.Active = 'Y' ")
        sql.AppendFormat("and r.Virtual = 'N' ")
        sql.AppendFormat("and ru.UserID is null ")

        If clsAuth.IsAdministrator() = True Then
            sql.AppendFormat("and (r.Parent in ({0}) or r.Parent is null) ", clsAuth.GetRoleList())
        Else
            sql.AppendFormat("and r.Parent in ({0}) ", clsAuth.GetRoleList())
        End If

        Me.gv.DataSource = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt")
        Me.gv.DataBind()

        Me.mpSearch.Show()
    End Sub

    Public ReadOnly Property SelectedRoles() As List(Of String)
        Get
            Dim roles As New List(Of String)

            For Each row As GridViewRow In Me.gv.Rows
                Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

                If TryCast(row.FindControl("chkRoleID"), CheckBox).Checked = True Then
                    roles.Add(key("RoleID"))
                End If
            Next

            Return roles
        End Get
    End Property

    Public Event Saving(ByVal sender As Object, ByVal e As System.EventArgs)
End Class
